//
//  LYVoiceMessageShowTool.m
//  BaChat
//
//  Created by 刘洋 on 16/6/19.
//  Copyright © 2016年 刘洋. All rights reserved.
//

#import "LYVoiceMessageShowTool.h"
#import "EMCDDeviceManager.h"

//正在执行动画的imageView
static BaseImageView *animatingImageView;


@implementation LYVoiceMessageShowTool





+(void)playWithMessage:(EMMessage *)message withMessageLabel:(BaseLabel *)messageLabel receiver:(BOOL)receiver{
    
//    把以前的动画移除,防止两个同时播放
    [animatingImageView stopAnimating];
    [animatingImageView removeFromSuperview];
    
    //播放语音
    //获取语音路径
    EMVideoMessageBody *voiceBody = [message.messageBodies lastObject];
    
    //本地语音文件路径
    NSString *path = voiceBody.localPath;
    
    //如果本地语音文件不存在,使用服务器语音
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        //文件消息体的远程服务器路径
        path = voiceBody.remotePath;
    }
    
 

    
    
    //播放的喇叭动画
    BaseImageView *ImageView = [[BaseImageView alloc]init];
    [messageLabel addSubview:ImageView];
    
    
    //判断消息是接收者还是发送者  yes 接收者  no 发送者
    if (receiver) {
        
        NSLog(@"我接受的语音");

        ImageView.animationImages = @[
                                      [UIImage imageNamed:@"messages_audio_animation_from1"],
                                      [UIImage imageNamed:@"messages_audio_animation_from2"],
                                      [UIImage imageNamed:@"messages_audio_animation_from3"]
                                      ];
        
        [ImageView mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.left.mas_equalTo(messageLabel.mas_left).offset(1);
            make.width.height.mas_equalTo(30);
            make.bottom.mas_equalTo(messageLabel.mas_bottom).offset(-10);
            
        }];
        
    }else{
        
        NSLog(@"我发送的语音");
      
        ImageView.animationImages = @[
                                      [UIImage imageNamed:@"messages_audio_animation_to1"],
                                      [UIImage imageNamed:@"messages_audio_animation_to2"],
                                      [UIImage imageNamed:@"messages_audio_animation_to3"]
                                      ];
        
       [ImageView mas_makeConstraints:^(MASConstraintMaker *make) {
           
           make.right.mas_equalTo(messageLabel.mas_right).offset(-1);
           make.width.height.mas_equalTo(30);
           make.bottom.mas_equalTo(messageLabel.mas_bottom).offset(-10);
           
       }];
        
        
    }
    
    
    //播放语音
    [[EMCDDeviceManager sharedInstance] asyncPlayingWithPath:path completion:^(NSError *error) {
        if (!error) {
            NSLog(@"语音播放完成");
            
            [animatingImageView stopAnimating];
            [animatingImageView removeFromSuperview];
            
            
        }
        
    }];
    
    
    
    
    
    
    ImageView.animationDuration = 1;
    [ImageView startAnimating];
    animatingImageView = ImageView;

    
}


+(void)stopAnimatingImage{
    
    //    把以前的动画移除,防止两个同时播放
    [animatingImageView stopAnimating];
    [animatingImageView removeFromSuperview];
    
}


+(NSString *)touchingPathFileWithTouchMessage:(EMMessage *)message{
    
    //播放语音
    //获取语音路径
    EMVideoMessageBody *voiceBody = [message.messageBodies lastObject];
    
    //本地语音文件路径
    NSString *path = voiceBody.localPath;
    
    //如果本地语音文件不存在,使用服务器语音
    NSFileManager *manager = [NSFileManager defaultManager];
    if (![manager fileExistsAtPath:path]) {
        //文件消息体的远程服务器路径
        path = voiceBody.remotePath;
    }
    
    NSString *pathfile = [NSString stringWithFormat:@"%@.wav",path];
    
    return pathfile;
    
}



@end
